package com.yongbao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yongbao.model.domain.PermissionDO;
import com.yongbao.model.domain.RolePermissionDO;
import com.yongbao.model.request.RolePermissionRequest;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author zhanghong
 */
@Repository
public interface RolePermissionMapper extends BaseMapper<RolePermissionDO> {
    RolePermissionDO selectDOByRoleId(Long roleId);
    List<RolePermissionDO> selectDOList(RolePermissionDO rolePermission);
    int insertDO(RolePermissionDO rolePermission);
    int batchInsertDO(List<RolePermissionDO> rolePermissionDOList);
    int updateDO(RolePermissionDO rolePermission);
    int deleteDOByRoleId(Long roleId);
    int deleteDOByRoleIds(String[] roleIds);

    @Select({"<script>",
            "   select count(1) from role_permission",
            "   <where>",
            "       deleted=0",
            "       <if test=\"1==0\">and id=#{param.id}</if>",
            "   </where>",
            "</script>"})
    long pageCount(@Param("param") RolePermissionRequest param);
    @Select({"<script>",
            "   select * from role_permission",
            "   <where>",
            "       deleted=0",
            "       <if test=\"1==0\">and id=#{param.id}</if>",
            "   </where>",
            "   order by create_time desc limit #{limit} offset #{offset}",
            "</script>"})
    List<RolePermissionDO> pageQuery(@Param("param") RolePermissionRequest param, @Param("limit") int limit, @Param("offset") long offset);

    @Select("select permission.* from role_permission right JOIN permission on role_permission.permission_id = permission.id where role_permission.role_id = #{roleId} ")
    List<PermissionDO> findByRoleId(@Param("roleId") long roleId);

}
